package com.g2room.driving.grap.life;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.ComShop;
import com.g2room.driving.grap.life.model.po.LifeFood;
import com.g2room.driving.grap.life.model.po.LifeFoodShop;
import com.g2room.driving.grap.life.model.po.LifeScenery;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.util.DbUtil;

public class GrapMafengwoJingdian {
	public String baseUrl = "http://you.ctrip.com";
	private String source = "携程";
	private static AtomicInteger id = new AtomicInteger(getMaxId());
	private static Map<String, Integer> typeMap = new HashMap<String, Integer>();
	public static void main(String[] args) throws Exception{
		GrapMafengwoJingdian gkd = new GrapMafengwoJingdian();
		gkd.getMeijing("http://www.mafengwo.cn/ajax/router.php?iMddid=10208&iPage=#ps#&iTagId=0&sAct=KMdd_StructWebAjax%7CGetPoisByTag", 36);
    }
	private int tid = 10016;
	private int getTypeId() {
		return tid++;
	}
	
//	private static synchronized int getId() {
//		id.addAndGet(1);
//		return id.get();
//	}
	
	/** * 美景 */
	public void getMeijing(String url, int pagesize) {
		for(int i = 1; i < pagesize+1; i++) {
			getMeiJingList(url.replace("#ps#", i+""), "美景");
		}
	}

	
	public void getMeiJingList(String url, String kind) {
		String content = null;
		try {
			content = GenericGrap.getJsonContent(url);
		} catch(Exception e) {
//			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent(kind);
			ge.setDescription("获取List 错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		// <a href=\"\/poi\/6653.html\"
		String[] strs = content.split("poi");
		List<String> urls = new ArrayList<String>();
		for(String str : strs) {
			if(str.contains(".html")) {
				String[] strs1 = str.split(".html");
				urls.add(strs1[0].replace("\\/", ""));
			}
		}
		List<LifeScenery> lses = new ArrayList<LifeScenery>();
		for(String durl : urls) {
			LifeScenery ls = null;
			try{
				ls = getMeiJingDetail("http://www.mafengwo.cn/poi/"+durl+".html");
				if(null != ls) {
					ls.setOldId(durl);
					lses.add(ls);
				}
			} catch(Exception e) {
				e.printStackTrace();
				GrapErr ge = new  GrapErr();
				ge.setUrl(url);
				ge.setType("6");
				ge.setContent(JSON.toJSONString(ls));
				ge.setDescription("获取Summary 错误："+e.getMessage());
				insert2ErrDb(ge);
			}
		}

		if(lses.size() > 0) insert2LsDb(lses);
	}
	public LifeScenery getMeiJingDetail(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent("美景");
			ge.setDescription("获取Deteal 错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		Elements els = doc.select(".wrapper .photo .bd img");
		LifeScenery ls = new LifeScenery();
		if(null != els && !els.isEmpty()) {
			int i = 0;
			String imgs = "";
			for(Element el : els) {
				if(i == 0) {
					ls.setCover(el.attr("src"));
				} else {
					if(imgs.length() > 0) imgs+=";";
					imgs += el.attr("src");
				}
				i++;
			}
			ls.setImages(imgs);
			
			els = doc.select(".title h1");
			if(null != els && !els.isEmpty()) {
				ls.setName(els.first().text());
			}
			els = doc.select(".summary");
			if(null != els && !els.isEmpty()) {
				ls.setDescription(els.first().html());
			}
			els = doc.select(".baseinfo .tel .content");
			if(null != els && !els.isEmpty()) {
				ls.setPhone(els.first().html());
			}
			els = doc.select(".baseinfo .item-time .content");
			if(null != els && !els.isEmpty()) {
				ls.setPlaytime(els.first().html());
			}
			Elements dtels = doc.select(".mod-detail dl dt");
			Elements ddels = doc.select(".mod-detail dl dd");
			if(null != dtels && !dtels.isEmpty()) {
				for(int j =0; j< dtels.size(); j++) {
					if(dtels.get(j).text().contains("交通")) {
						ls.setTraffic(ddels.get(j).html());
					} else if(dtels.get(j).text().contains("门票")) {
						ls.setTicketPrice(ddels.get(j).text());
					} else if(dtels.get(j).text().contains("开放时间")) {
						ls.setShopHours(ddels.get(j).text());
					}
				}
			}
			els = doc.select(".mod-location .mhd .sub");
			if(null != els && !els.isEmpty()) {
				ls.setAddress(els.first().text());
			}
			els = doc.select(".baseinfo .tel .content");
			if(null != els && !els.isEmpty()) {
				ls.setPhone(els.first().html());
			}
			els = doc.select(".mod-detail");
			if(null != els && !els.isEmpty()) {
				if(els.size()>1) ls.setTips(els.get(1).html());
			}
			ls.setKindName("美景");
			ls.setSource("蚂蜂窝");
			ls.setSourceUrl(url);
			return ls;
		}
		return null;
	}
	
	public void insert2LsDb(List<LifeScenery> lses) {
		String sql = "insert into life_scenery(playtime,source_url,category_name,type_name,kind_name,type_ids,score,ticket_price,rank,tips,address,images,category_ids,shop_hours,source,cover,traffic,website,province_name,name,phone,description,cost,interest,scenery,city_name,level_name,full_name) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(LifeScenery cc : lses) {
				
				ps.setString(1, cc.getPlaytime());
				ps.setString(2, cc.getSourceUrl());
				ps.setString(3, cc.getCategoryName());
				ps.setString(4, cc.getTypeName());
				ps.setString(5, cc.getKindName());
				ps.setString(6, null);
				ps.setString(7, cc.getScore());
				ps.setString(8, cc.getTicketPrice());
				ps.setString(9, cc.getRank());
				ps.setString(10, cc.getTips());
				ps.setString(11, cc.getAddress());
				ps.setString(12, cc.getImages());
				ps.setString(13, null);
				ps.setString(14, cc.getShopHours());
				ps.setString(15, cc.getSource());
				ps.setString(16, cc.getCover());
				ps.setString(17, cc.getTraffic());
				ps.setString(18, cc.getWebsite());
				ps.setString(19, cc.getProvinceName());
				ps.setString(20, cc.getName());
				ps.setString(21, cc.getPhone());
				ps.setString(22, cc.getDescription());
				ps.setString(23, cc.getCost());
				ps.setString(24, cc.getInterest());
				ps.setString(25, cc.getScenery());
				ps.setString(26, cc.getCityName());
				ps.setString(27, cc.getLevelName());
				ps.setString(28, cc.getFullName());

//				ps.setString(1, ls.getOldId());
//				ps.setString(2, ls.getPlaytime());
//				ps.setInt(3, 0);
//				ps.setString(4, ls.getSourceUrl());
//				ps.setString(5, ls.getCategoryName());
//				ps.setInt(6, null == ls.getCityId() ? 0 : ls.getCityId());
//				ps.setString(7, ls.getTypeName());
//				ps.setString(8, ls.getKindName());
//				ps.setString(9, ls.getScore());
//				ps.setString(10, ls.getTicketPrice());
//				ps.setString(11, ls.getRank());
//				ps.setString(12, ls.getTips());
//				ps.setString(13, ls.getAddress());
//				ps.setString(14, null);
//				ps.setString(15, ls.getShopHours());
//				ps.setInt(16, null == ls.getProvinceId() ? 0 : ls.getProvinceId());
//				ps.setString(17, ls.getSource());
//				ps.setString(18, ls.getCoverId());
//				ps.setString(19, ls.getCover());
//				ps.setString(20, ls.getTraffic());
//				ps.setString(21, ls.getWebsite());
//				ps.setString(22, ls.getProvinceName());
//				ps.setString(23, ls.getName());
//				ps.setString(24, null);
//				ps.setString(25, null);
//				ps.setString(26, null);
//				ps.setString(27, ls.getPhone());
//				ps.setString(28, ls.getDescription());
//				ps.setInt(29, null == ls.getKindId() ? 0 : ls.getKindId());
//				ps.setString(30, ls.getCityName());
//				ps.setString(31, ls.getLevelName());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lses));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public void insert2LsfDb(List<LifeFood> lfs) {
		String sql = "insert into life_food(source_url,kind_name,cover,province_name,images,name,description,city_name,source,id) values(?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(LifeFood cc : lfs) {
				ps.setString(1, cc.getSourceUrl());
				ps.setString(2, cc.getKindName());
				ps.setString(3, cc.getCover());
				ps.setString(4, cc.getProvinceName());
				ps.setString(5, cc.getImages());
				ps.setString(6, cc.getName());
				ps.setString(7, cc.getDescription());
				ps.setString(8, cc.getCityName());
				ps.setString(9, cc.getSource());
				ps.setInt(10, cc.getId());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lfs));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	
	private String getUtf8Str(String str) {
		try {
			str = null == str ? null : new String(str.getBytes(), "utf-8").replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "").replaceAll("[\\x{10000}-\\x{10FFFF}]", "");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public void insert2FoodShop(List<LifeFoodShop> lfss) {
		PreparedStatement grapPs = null;
		try {
			
			grapPs = DbUtil.getComConn().prepareStatement("insert into life_food_shop(name,food_id,food_name,cover,old_id) values(?,?,?,?,?)");
			for(LifeFoodShop lfs : lfss) {
				grapPs.setString(1, lfs.getName());
				grapPs.setInt(2, lfs.getFoodId());
				grapPs.setString(3, lfs.getFoodName());
				grapPs.setString(4, lfs.getCover());
				grapPs.setInt(5,null ==  lfs.getOldId() ? 0 : lfs.getOldId());
				grapPs.addBatch();
			}
			grapPs.executeBatch();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public void insert2ShopDb(List<ComShop> shops) {
		String sql = "insert into com_shop(old_id,category_id,source_url,category_name,city_id,type_name,kind_name,address,flavor,second_name,qualification,province_id,tags,shop_hours,full_name,name,source,star,menu,cover,brand_story,province_name,district_name,place_name,service,spend_perhead,type_id,environment,phone,description,kind_id,list,city_name,traffic) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(ComShop ls : shops) {
				ps.setString(1, ls.getOldId());
				ps.setInt(2, null == ls.getCategoryId() ? 0 : ls.getCategoryId() );
				ps.setString(3, ls.getSourceUrl());
				ps.setString(4, ls.getCategoryName());
				ps.setInt(5, null == ls.getCityId() ? 0 : ls.getCityId() );
				ps.setString(6, ls.getTypeName());
				ps.setString(7, ls.getKindName());
				ps.setString(8, ls.getAddress());
				ps.setString(9, ls.getFlavor());
				ps.setString(10, ls.getSecondName());
				ps.setString(11, ls.getQualification());
				ps.setInt(12, null == ls.getProvinceId() ? 0 : ls.getProvinceId() );
				ps.setString(13, ls.getTags());
				ps.setString(14, ls.getShopHours());
				ps.setString(15, ls.getFullName());
				ps.setString(16, ls.getName());
				ps.setString(17, ls.getSource());
				ps.setString(18, ls.getStar());
				ps.setString(19, ls.getMenu());
				ps.setString(20, ls.getCover());
				ps.setString(21, ls.getBrandStory());
				ps.setString(22, ls.getProvinceName());
				ps.setString(23, ls.getDistrictName());
				ps.setString(24, ls.getPlaceName());
				ps.setString(25, ls.getService());
				ps.setString(26, ls.getSpendPerhead());
				ps.setInt(27, null == ls.getTypeId() ? 0 : ls.getTypeId() );
				ps.setString(28, ls.getEnvironment());
				ps.setString(29, ls.getPhone());
				ps.setString(30, ls.getDescription());
				ps.setInt(31, null == ls.getKindId() ? 0 : ls.getKindId() );
				ps.setString(32, ls.getList());
				ps.setString(33, ls.getCityName());
				ps.setString(34, ls.getTraffic());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("14");
			ge.setContent(JSON.toJSONString(shops));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static List<GrapErr> getErrs() {
		List<GrapErr> univs = new ArrayList<GrapErr>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select * from grap_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				GrapErr univ = new GrapErr();
				univ.setId(rs.getInt("id"));
				univ.setUrl(rs.getString("url"));
				univ.setType(rs.getString("type"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	
	public static void delErr(int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("delete from grap_err where id=?");
			grapPs.setInt(1, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static void updateLs(LifeScenery ls) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("update life_scenery set cost =?,interest =?,scenery =?  where id=?");
			grapPs.setString(1, ls.getCost());
			grapPs.setString(2, ls.getInterest());
			grapPs.setString(3, ls.getScenery());
			grapPs.setInt(4, ls.getId());
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static Map<Integer, String> getSensList() {
		Map<Integer, String> map =new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select  id,source_url from life_scenery";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				map.put(rs.getInt("id") ,rs.getString("source_url"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	private static synchronized int getId() {
		id.addAndGet(1);
		return id.get();
	}
	
	public static int getMaxId() {
		int r = 1;
		try {
			Statement statement = DbUtil.getComConn().createStatement();
			String sql = "select max(id) as id from life_food";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				r = rs.getInt("id");
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
}
